#ifndef COORDINATE2DWIDGET_H
#define COORDINATE2DWIDGET_H

#include <QWidget>
#include "splinecurve.h"

class Coordinate2DWidget : public QWidget
{
    Q_OBJECT
public:

    explicit Coordinate2DWidget(QWidget *parent = nullptr);

private:
    void init();
    QPoint transformBack(const QPointF& pt);   //显示坐标转化为像素坐标
    QPointF transformTo(const QPoint& pt);     //像素坐标转化为计算坐标
    void drawAxisNum();
    void drawCurves();
    void drawCurve(const SplineCurve::ptr& curve);
    void drawPoint(const SplinePoint::ptr& sp);
    void drawSelectionRect();
    void selectPoints(bool isSingle, bool isAdded);
    void clearSelectPoints();
    void movePoints();
    void moveViewRect();

    bool isInViewRect(const QPointF& pt) const;

    void onAddKey();
    void onDeleteKey();
    void onTangentModeSelect(QAction* action);
private:
    bool m_hasInit = false;
    int m_marginCooridate = 48;           //显示坐标数值的空间大小
    int m_separationSpace = 40;
    int m_iconSize = 8;                   //点图标大小
    qreal m_unit = 0.1;
    qreal m_zoom = 1.0;
    QPoint m_offset;

    QRectF m_viewRect;
    QRect  m_ChartRect;

    std::vector<SplineCurve::ptr> m_curves;
    SplineCurve::ptr m_activeCurve;

    QPoint m_pressPosition;
    QPoint m_movePosition;
    int m_mouseBtn = Qt::NoButton;
    int m_keyBtn = 0;

    QMenu *m_pMenu;
    QAction *m_actAddKey;
    QAction *m_actDeleteKey;
    QActionGroup *m_grpTangentMode;
    QAction *m_actLinear;
    QAction *m_actConstant;
    QAction *m_actWeighted;
    QAction *m_actAuto;
    QAction *m_actUser;
    QAction *m_actBreak;

signals:

    // QWidget interface
protected:
    void paintEvent(QPaintEvent *event);
    void keyPressEvent(QKeyEvent *event);
    void keyReleaseEvent(QKeyEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void wheelEvent(QWheelEvent *event);
    void contextMenuEvent(QContextMenuEvent *event);


};

#endif // COORDINATE2DWIDGET_H
